package com.yunsc.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.common.utils.PageResult;
import com.yunsc.store.dao.YunscHelpDao;
import com.yunsc.store.dto.StoreHelpDTO;
import com.yunsc.store.entity.YunscHelpEntity;
import com.yunsc.store.service.YunscHelpService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


@Service("yunscHelpService")
public class YunscHelpServiceImpl extends ServiceImpl<YunscHelpDao, YunscHelpEntity> implements YunscHelpService {


    @Override
    public PageResult<YunscHelpEntity> queryHelpList(Integer page, Integer storeId) {
        LambdaQueryWrapper<YunscHelpEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscHelpEntity::getStoreId, storeId);
        lqw.eq(YunscHelpEntity::getIsDelete, 0);
        lqw.orderByAsc(YunscHelpEntity::getSort);
        Page<YunscHelpEntity> pageResult = this.page(new Page<>(page, 15), lqw);
        return PageResult.formatPageResult(pageResult.getRecords(), page, 15, pageResult.getTotal());
    }

    @Override
    public void editHelp(StoreHelpDTO params, Integer storeId) {
        YunscHelpEntity help = this.getById(params.getHelpId());
        if (help == null) {
            throw new YunscException("该帮助不存在");
        }
        YunscHelpEntity form = params.getForm();
        BeanUtils.copyProperties(form, help);
        form.setHelpId(help.getHelpId());
        this.updateById(form);
    }

    @Override
    public void deleteHelp(String helpId, Integer storeId) {
        YunscHelpEntity help = this.getById(helpId);
        if (help == null) {
            throw new YunscException("该帮助不存在");
        }
        help.setIsDelete(1);
        this.updateById(help);
    }

    @Override
    public void addHelp(StoreHelpDTO params, Integer storeId) {
        YunscHelpEntity help = params.getForm();
        help.setStoreId(storeId);
        help.setIsDelete(0);
        this.save(help);
    }

}